package com.sdtele.tta.operation;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.sdtele.tta.dac.Dac;
import com.sdtele.tta.models.JsonRequest;
import com.sdtele.tta.models.JsonResponse;
import com.sdtele.tta.models.Team;
import com.sdtele.tta.models.User;
import com.sdtele.tta.utils.TeamUtils;

/**
 * 操作类实现：GetUser(json.operation=getUser)
 * 
 * @author XCream
 */
public class OperationAddTeam extends Operation {

	@Override
	public JsonResponse handle(JsonRequest jsonRequest) {
		JsonResponse jres = new JsonResponse();
		User curUser = (User) request.getSession().getAttribute("user");
		if ((!curUser.equals(null)) && curUser.getIsManager() == 1) {
			String teamName = jsonRequest.getJsonObject().get("teamName").getAsString();
			long upTeamId = jsonRequest.getJsonObject().get("upTeamId").getAsLong();
			String cityName = jsonRequest.getJsonObject().get("cityName").getAsString();
			int cityId = jsonRequest.getJsonObject().get("cityId").getAsInt();
			Set<Long> teamIdSet = new HashSet<Long>();
			teamIdSet.add(curUser.getTeamId());
			TeamUtils.teamAuthority(curUser.getTeamId(), teamIdSet);
			if (teamIdSet.contains(upTeamId)) {
				Team team = new Team();
				team.setTeamName(teamName);
				team.setUpTeamId(upTeamId);
				team.setCityName(cityName);
				team.setCityID(cityId);
				Dac.getInstance().addTeam(team);
				if (team.getId() > 0) {
					jres.setStatus(0);
					jres.setMessage("添加组织成功");
					jres.setEntity(team);
				} else {
					jres.setStatus(1);
					jres.setMessage("未知错误，添加组织失败");
				}
			} else {
				jres.setStatus(1);
				jres.setMessage("权限不够添加该组织");
			}
		} else {
			jres.setStatus(1);
			jres.setMessage("未登录状态或者不具备添加组织权限");
		}
		return jres;
	}

}
